package com.yunhe.sms.controller.system;

import com.yunhe.commons.dto.Pageable;
import com.yunhe.commons.dto.TreeNode;
import com.yunhe.sms.entity.system.Role;
import com.yunhe.sms.service.system.PermissionService;
import com.yunhe.sms.service.system.RoleService;
import com.yunhe.sms.service.system.impl.PermissionServiceImpl;
import com.yunhe.sms.service.system.impl.RoleServiceImpl;
import com.yunhe.webmvc.annotation.RequestBean;
import com.yunhe.webmvc.annotation.RequestMapping;
import com.yunhe.webmvc.annotation.ResponseJson;
import com.yunhe.webmvc.model.JSONResponse;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by Administrator on 2017/10/23 0023.
 */
@RequestMapping("/role")
public class RoleController {

    private  RoleService roleService = new RoleServiceImpl();
    private  PermissionService permissionService = new PermissionServiceImpl();
    @RequestMapping
    public String list(){
        return "system/role/list";
    }

    @RequestMapping("/listData")
    @ResponseJson
    public Pageable listData(Integer offset, Integer limit){
        Pageable<Role> pageable = roleService.findPage(offset, limit);
        return  pageable;
    }

    @RequestMapping("/add")
    public String add(){
        return "system/role/add";
    }

    @RequestMapping("/edit")
    public String edit(Long id, HttpServletRequest req){
        Role role = roleService.find(id);
        req.setAttribute("role",role);
        return "system/role/edit";
    }

    @RequestMapping("/save")
    public String save(@RequestBean Role role,String permissionIds){
        roleService.save(role,permissionIds);
        return "redirect:/role";
    }

    @RequestMapping("/delete")
    @ResponseJson
    public JSONResponse delete(String ids){
        roleService.delete(ids);
        return JSONResponse.success("删除成功");
    }

    @RequestMapping("/update")
    public String update(@RequestBean Role role,String permissionIds){
        roleService.update(role,permissionIds);
        return "redirect:/role";
    }

    @RequestMapping("/tree")
    @ResponseJson
    public JSONResponse tree(Long roleId){
        List<TreeNode> treeNodes = permissionService.findTreeNodes(roleId);
        return JSONResponse.success("权限树节点获取成功",treeNodes);
    }
}
